

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;


public class Client {
    private final String address = "127.0.0.1";//服务端地址
    private String name;//用户姓名

    private final int port = 9999;//服务端端口
    private SocketChannel socketChannel;
    private static List<String> memberList = new ArrayList<>();

    public void init(String name) {
        try {
            this.name = name;
            socketChannel = SocketChannel.open(new InetSocketAddress(address,port));
            socketChannel.configureBlocking(false);
            new Thread(()->{
                while (true){
                    try {
                        ByteBuffer buffer = ByteBuffer.allocate(1024);
                        int readLen = 0;
                        String msg = "";
                        while ((readLen = socketChannel.read(buffer))>0){
                            buffer.flip();
                            msg += new String(buffer.array(),0,readLen);
                            buffer.clear();
                        }
                        if(msg.contains("%^&")){  // 客户端接收消息 该消息标识服务端返回新的注册成员
                            String members = msg.substring(msg.indexOf("%^&")+3);
                            if (members.length()>0){
                                String[] split = members.split("#");
                                memberList = new ArrayList<>();
                                for (String s:
                                split) {
                                    if(!name.equals(s)){
                                        memberList.add(s);
                                    }
                                }
                                //对成员列表进行UI更新
                                ChatRoomUi.changeMerberUi(memberList);

                            }
                        }else if(msg.contains("#@!")){//该标识表示返回群聊信息
                            ChatRoomUi.changeMessageUi(msg);
                        }else if(msg.contains("@!#")){//该标识表示返回私聊信息

                            ChatRoomUi.changeTaklToUi(msg);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
            ).start();
        }catch (Exception e){
            e.printStackTrace();
        }

    }



    public void sendMessage(String msg){
        ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());//
        int len = 1024; //缓冲长度1024
        byte[] bytes = new byte[len];
        while (buffer.hasRemaining()){
            int sendLen = Math.min(len,buffer.remaining());//获取数据总量和  每次传输的大小对比(如果数据总量小于1024一次性就传完了)
            buffer.get(bytes,0,sendLen);//传输数据组装到bytes
            try {
                socketChannel.write(ByteBuffer.wrap(bytes,0,sendLen));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public List<String> getMember(){
        return memberList;
    }

}
